package com.zhoug.common3.adapters.recycler.dividers.sticky;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import androidx.annotation.ColorInt;

/**
 * 文本粘性头部
 *适用于{@link androidx.recyclerview.widget.LinearLayoutManager} 布局
 * @Author 35574
 * @Date 2021/3/29
 * @Description
 */
public class ListStickyTextDecoration extends ListStickyDecoration<String> {
    /**
     * 头部的高度
     */
    private int headerHeight = 100;
    /**
     * 头部的背景色
     */
    private @ColorInt
    int headerBackgroundColor = 0xFFEEEEEE;
    /**
     * 头部文字的颜色
     */
    private @ColorInt
    int textColor = 0xFF333333;

    /**
     * 头部文字大小
     */
    private float textSize = 50;
    /**
     * 文字是否加粗
     */
    private boolean textBold = true;
    /**
     * 文字左边距
     */
    private float paddingStart = 30;
    /**
     * 文字是否居中:默认false
     */
    private boolean gravityCenter;


    private Paint mPaint;
    private Paint.FontMetrics mFontMetrics;

    private ListStickyTextDecoration() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mFontMetrics=new Paint.FontMetrics();
    }

    private void init() {
    }


    @Override
    protected int getHeaderHeight() {
        return headerHeight;
    }


    @Override
    protected void onDrawHeader(Canvas canvas, Rect headerRect, GroupInfo<String> groupInfo) {
        //绘制背景
        mPaint.setColor(headerBackgroundColor);
        canvas.drawRect(headerRect,mPaint);
        //绘制文字
        mPaint.setColor(textColor);
        mPaint.setFakeBoldText(textBold);
        mPaint.setTextSize(textSize);

        mPaint.getFontMetrics(mFontMetrics);
        float x=headerRect.left+paddingStart;
        float baseLine=((headerRect.top + headerRect.bottom) >> 1) -(mFontMetrics.descent+mFontMetrics.ascent)/2;;
        if(gravityCenter){
            mPaint.setTextAlign(Paint.Align.CENTER);
            x= (headerRect.left + headerRect.right) >> 1;
        }else{
            mPaint.setTextAlign(Paint.Align.LEFT);
        }


        canvas.drawText(groupInfo.getData(),x,baseLine,mPaint);
    }

    public static class Builder {
        /**
         * 头部的高度
         */
        private int headerHeight = 100;
        /**
         * 头部的背景色
         */
        private @ColorInt
        int headerBackgroundColor = 0xFFEEEEEE;
        /**
         * 头部文字的颜色
         */
        private @ColorInt
        int textColor = 0xFF333333;

        /**
         * 头部文字大小
         */
        private float textSize = 50;
        /**
         * 文字是否加粗
         */
        private boolean textBold = true;
        /**
         * 文字左边距
         */
        private float paddingStart = 30;
        /**
         * 文字是否居中:默认false
         */
        private boolean gravityCenter;

        public Builder setHeaderHeight(int headerHeight) {
            this.headerHeight = headerHeight;
            return this;
        }

        public Builder setHeaderBackgroundColor(int headerBackgroundColor) {
            this.headerBackgroundColor = headerBackgroundColor;
            return this;
        }

        public Builder setTextColor(int textColor) {
            this.textColor = textColor;
            return this;
        }

        public Builder setTextSize(float textSize) {
            this.textSize = textSize;
            return this;
        }

        public Builder setTextBold(boolean textBold) {
            this.textBold = textBold;
            return this;
        }

        public Builder setPaddingStart(float paddingStart) {
            this.paddingStart = paddingStart;
            return this;
        }

        public Builder setGravityCenter(boolean gravityCenter) {
            this.gravityCenter = gravityCenter;
            return this;
        }

        public ListStickyTextDecoration build() {
            ListStickyTextDecoration decoration = new ListStickyTextDecoration();
            decoration.headerHeight = headerHeight;
            decoration.headerBackgroundColor = headerBackgroundColor;
            decoration.textColor = textColor;
            decoration.textSize = textSize;
            decoration.textBold = textBold;
            decoration.gravityCenter = gravityCenter;
            decoration.paddingStart = paddingStart;
            decoration.init();
            return decoration;
        }
    }

}
